// const AV = require('../../utils/av-weapp.js')

const util = require('../../utils/util.js')
const app = getApp();
Page({
	data:{
		carts: [],
		minusStatuses: [],
		selectedAllStatus: true,
		total: '',
		startX: 0,
		itemLefts: [],
    total:0
	},
	// bindMinus: function(e) {
	// 	// loading提示
	// 	wx.showLoading({
	// 		title: '操作中',
	// 		mask: true
	// 	});
	// 	var index = parseInt(e.currentTarget.dataset.index);
	// 	var num = this.data.carts[index].get('quantity');
	// 	// 如果只有1件了，就不允许再减了
	// 	if (num > 1) {
	// 		num --;
	// 	}
	// 	// 只有大于一件的时候，才能normal状态，否则disable状态
	// 	var minusStatus = num <= 1 ? 'disabled' : 'normal';
	// 	// 购物车数据
	// 	var carts = this.data.carts;
	// 	carts[index].set('quantity', num);
	// 	// 按钮可用状态
	// 	var minusStatuses = this.data.minusStatuses;
	// 	minusStatuses[index] = minusStatus;
	// 	// 将数值与状态写回
	// 	this.setData({
	// 		carts: carts,
	// 		minusStatuses: minusStatuses
	// 	});
	// 	// update database
	// 	carts[index].save().then(function () {
	// 		wx.hideLoading();
	// 	});
	// 	this.sum();
	// },
	bindPlus: function(e) {
		let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let param = {cid:id};
    let that = this;

    let data = this.data.carts;
    data[index]['num'] = data[index]['num']+1;
    that.setData({
      carts: data
    })
    util.commonRequest("index/Cart/addNum",'post',param,function(res){
      that.sum();
    })
   
    
		
	},
  bindMinus: function(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let param = { cid: id };
    let that = this;
    util.commonRequest("index/Cart/manualNum", 'post', param, function (res) {
     
      if(res.data.code == 200){

        let dataq = that.data.carts;
        dataq[index]['num'] = dataq[index]['num'] - 1;

        that.setData({
          carts: dataq
        })
        that.sum();
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    })
	},
	bindManualTapped: function() {
		// 什么都不做，只为打断跳转
	},
	bindCheckbox: function(e) {
		let index = e.currentTarget.dataset.index;
    let data = this.data.carts;
    data[index]['selected'] = !data[index]['selected'];
    this.setData({
      carts:data
    })
    let data1 = this.data.carts;
    let len1 = data1.length;
    // debugger
    let ii = 0;
    for(let i =0; i<data1.length; i++){
      if (data1[i]['selected'] == true){
        ii++;
      }
    }
    if (ii == 0){
      this.setData({
        selectedAllStatus:false
      })
    } 
    if (ii == len1){
      this.setData({
        selectedAllStatus: true
      })
    }
		this.sum();
	},
	bindSelectAll: function() {
		let data = this.data.carts;
    let selectedAllStatus = this.data.selectedAllStatus;
    if (selectedAllStatus){
      for(let i = 0; i < data.length; i++){
        data[i]['selected'] = false;
      }
    } else{
      for (let i = 0; i < data.length; i++) {
        data[i]['selected'] = true;
      }
    }
    this.setData({
      selectedAllStatus: !selectedAllStatus,
      carts:data
    })
		this.sum();

	},
	bindCheckout: function() {
    let data = this.data.carts;
    let strId = '';
    for (let i = 0; i<data.length; i++){
      if (data[i]['selected'] == true){
        strId+= data[i]['id']+',';
      }
    }
    strId = strId.substr(0, strId.length - 1);
    if(!strId){
      wx.showToast({
        title: '请选择购买商品',
        icon:'none'
      })
      return;
    }
		wx.navigateTo({
      url: '../pay/pay?strId=' + strId
		});
	},
	delete: function (e) {
		var that = this;
	  let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let param = {cid:id};
    util.commonRequest('index/Cart/delectCart','post',param,function(res){
      if (res.data.code == 200)
      {
        wx.showToast({
          title: res.data.msg,
        })
        carts.splice(index, 1);
        that.setData({
          carts:carts
        })
        that.sum();
      } else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    })
	},
	reloadData: function() {
    let that = this;
    let param = { user_id: app.globalData.userInfo.id};
    util.commonRequest('index/Cart/cartList','post',param,function(res){
      if (res.data.code == 200)
      {
        // debugger
        that.setData({
          carts:res.data.data.data,
          total: res.data.data.total
        })
      }
    })
	},
	onShow: function() {
		this.reloadData();
	},
	sum: function() {
    let that = this;
    let carts = that.data.carts;
    let strid = '';

    for(let i =0; i<carts.length; i++)
    {
      if (carts[i]['selected']){
        strid += carts[i]['id']+',';
      }
    }
    let param = { strid: strid, user_id: app.globalData.userInfo.id};
    util.commonRequest('/index/Cart/cartTotal','post',param,function(res){
      if (res.data.code == 200){
        that.setData({
          total:res.data.data
        })
      }
    })
	},
	showGoods: function (e) {
		// 点击购物车某件商品跳转到商品详情
		var objectId = e.currentTarget.dataset.objectId;
		wx.navigateTo({
      url: '../goods/detail/detail?goodsId=' + objectId
		});
	},
	touchStart: function (e) {
		var startX = e.touches[0].clientX;
		this.setData({
			startX: startX,
			itemLefts: []
		});
	},
	touchMove: function (e) {
		var index = e.currentTarget.dataset.index;
		var movedX = e.touches[0].clientX;
		var distance = this.data.startX - movedX;
		var itemLefts = this.data.itemLefts;
		itemLefts[index] = -distance;
		this.setData({
			itemLefts: itemLefts
		});
	},
	touchEnd: function (e) {
		var index = e.currentTarget.dataset.index;
		var endX = e.changedTouches[0].clientX;
		var distance = this.data.startX - endX;
		// button width is 60
		var buttonWidth = 60;
		if (distance <= 0) {
			distance = 0;
		} else {
			if (distance >= buttonWidth) {
				distance = buttonWidth;
			} else if (distance >= buttonWidth / 2){
				distance = buttonWidth;
			} else {
				distance = 0;
			}
		}
		var itemLefts = this.data.itemLefts;
		itemLefts[index] = -distance;
		this.setData({
			itemLefts: itemLefts
		});
	}
})